﻿using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.Text;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public partial class Admin_Admin_AddAdmin : Admin, IRequiresSessionState
{
    private AdminGroupBll _adminGroup = null;
    private AdminBll _admin = null;
    protected AdminGroupBll m_adminGroup
    {
        get
        {
            AdminGroupBll adminGroup;
            if (this._adminGroup == null)
            {
                this._adminGroup = new AdminGroupBll(this.conn);
                adminGroup = this._adminGroup;
            }
            else
            {
                adminGroup = this._adminGroup;
            }
            return adminGroup;
        }
    }
    protected AdminBll m_admin
    {
        get
        {
            AdminBll admin;
            if (this._admin == null)
            {
                this._admin = new AdminBll(this.conn);
                admin = this._admin;
            }
            else
            {
                admin = this._admin;
            }
            return admin;
        }
    }
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        int iD = Fetch.GetID("AdminID");
        SiteConfigBll siteConfigBll = new SiteConfigBll();
        if (!base.admin.CheckSuperAdmin())
        {
            base.WriteLog("View", Language.GetAdminMsg("Log/Access", iD.ToString()), "Administrator");
            base.WriteRightErrMsg();
        }
        if (!base.IsPostBack)
        {
            if (siteConfigBll.GetSiteString("SiteConfigInfo/CityArea") == "0")
            {
                this.trCityArea_1.Visible = false;
                this.trCityArea_2.Visible = false;
            }
            else
            {
                this.ProvinceBind();
            }
            DataTable allAdminGroup = this.m_adminGroup.GetAllAdminGroup();
            this.ListGroup.DataSource = allAdminGroup;
            this.ListGroup.DataTextField = "AdminGroupName";
            this.ListGroup.DataValueField = "AdminGourpID";
            this.TxtEndDate.Attributes.Add("onClick", string.Concat(new string[]
			{
				"show_cele_date(",
				this.TxtEndDate.UniqueID,
				",'','',",
				this.TxtEndDate.UniqueID,
				");"
			}));
            this.TxtLastLogin.Attributes.Add("onClick", string.Concat(new string[]
			{
				"show_cele_date(",
				this.TxtLastLogin.UniqueID,
				",'','',",
				this.TxtLastLogin.UniqueID,
				");"
			}));
            this.ListGroup.DataBind();
            this.ListGroup.Items.Remove(new ListItem("分站管理员", "1"));
            if (iD != 0)
            {
                this.BindBaseData(iD);
                this.ButUpdate.Visible = true;
                this.ButAdd.Visible = false;
                this.passtishi.Visible = true;
            }
            else
            {
                this.TxtLoginDate.Text = (this.TxtLastLogin.Text = DateTime.Now.ToString());
                this.TxtLastLogin.ReadOnly = (this.TxtLastLoginIP.ReadOnly = (this.TxtLoginIP.ReadOnly = true));
                this.TxtEndDate.Text = DateTime.Now.AddDays(365.0).ToShortDateString();
                this.TxtLoginTimes.Text = "0";
                this.passtishi.Visible = false;
            }
        }
        if (siteConfigBll.GetSiteString("SiteConfigInfo/CityArea") == "0")
        {
            this.CheckAccess.Attributes.Add("onClick", "return ShowRowPower()");
        }
        else
        {
            this.CheckAccess.Attributes.Add("onClick", "return ShowRowPowerByCity()");
        }
        this.CheckCityArea.Attributes.Add("onClick", "return ShowPower()");
    }
    protected void ProvinceBind()
    {
        Shop_City shop_City = new Shop_City(this.conn);
        this.DropDownList_Province.DataSource = shop_City.GetAllProvince();
        this.DropDownList_Province.DataBind();
        this.DropDownList_Province.Items.Insert(0, new ListItem("－请选择－", ""));
        this.DropDownList_City.Items.Insert(0, new ListItem("－请选择－", ""));
    }
    protected void DropDownList_Province_SelectedIndexChanged(object sender, EventArgs e)
    {
        Shop_City shop_City = new Shop_City(this.conn);
        this.DropDownList_City.DataSource = shop_City.GetCityByProvince(this.DropDownList_Province.SelectedValue);
        this.DropDownList_City.DataBind();
        this.DropDownList_City.Items.Insert(0, new ListItem("－请选择－", ""));
    }
    protected void BindBaseData(int AdminID)
    {
        DataTable adminByAdminID = this.m_admin.GetAdminByAdminID(AdminID);
        if (adminByAdminID.Rows.Count > 0)
        {
            DataTable adminGroupByAdminID = this.m_adminGroup.GetAdminGroupByAdminID(AdminID);
            for (int i = 0; i < adminGroupByAdminID.Rows.Count; i++)
            {
                foreach (ListItem listItem in this.ListGroup.Items)
                {
                    if (listItem.Value.Trim() == adminGroupByAdminID.Rows[i]["AdminGourpID"].ToString().Trim())
                    {
                        listItem.Selected = true;
                    }
                }
            }
            SiteConfigBll siteConfigBll = new SiteConfigBll();
            if (siteConfigBll.GetSiteString("SiteConfigInfo/CityArea") != "0")
            {
                DataTable adminUserGroupByAdminID = this.m_admin.GetAdminUserGroupByAdminID(AdminID);
                if (adminUserGroupByAdminID.Rows.Count > 0)
                {
                    if (adminUserGroupByAdminID.Rows[0]["GroupID"].ToString() == "1")
                    {
                        this.CheckCityArea.Checked = true;
                        this.rowpower.Attributes.Add("style", "display:none;");
                        this.trCityArea_2.Attributes.Add("style", "display:block;");
                        this.DropDownList_Province.Items.FindByValue(adminByAdminID.Rows[0]["ProvinceArea"].ToString()).Selected = true;
                        if (adminByAdminID.Rows[0]["CityArea"].ToString() != "")
                        {
                            Shop_City shop_City = new Shop_City(this.conn);
                            this.DropDownList_City.DataSource = shop_City.GetCityByProvince(this.DropDownList_Province.SelectedValue);
                            this.DropDownList_City.DataBind();
                            this.DropDownList_City.Items.Insert(0, new ListItem("－请选择－", ""));
                            this.DropDownList_City.Items.FindByValue(adminByAdminID.Rows[0]["CityArea"].ToString()).Selected = true;
                        }
                    }
                }
            }
            string power = adminByAdminID.Rows[0]["Power"].ToString();
            if (adminByAdminID.Rows[0]["Access"].ToString() == "1")
            {
                this.CheckAccess.Checked = true;
                this.rowpower.Style.Add("display", "none");
            }
            this.TxtRemark.Text = adminByAdminID.Rows[0]["Remark"].ToString();
            this.TxtAcceptIP.Text = adminByAdminID.Rows[0]["AcceptIP"].ToString();
            this.TxtEndDate.Text = adminByAdminID.Rows[0]["EndDate"].ToString();
            this.TxtLastLogin.Text = adminByAdminID.Rows[0]["LastLogin"].ToString();
            this.TxtLastLoginIP.Text = adminByAdminID.Rows[0]["LastLoginIP"].ToString();
            this.TxtLoginDate.Text = adminByAdminID.Rows[0]["LoginDate"].ToString();
            this.TxtLoginIP.Text = adminByAdminID.Rows[0]["LoginIP"].ToString();
            this.TxtLoginTimes.Text = adminByAdminID.Rows[0]["LoginTimes"].ToString();
            this.TxtUserName.Text = adminByAdminID.Rows[0]["UserName"].ToString();
            this.SetPower(power);
            if (adminByAdminID.Rows[0]["disable"].ToString().ToLower() == "true")
            {
                this.CheckDisable.Checked = true;
            }
        }
    }
    protected void Button_Command(object sender, CommandEventArgs e)
    {
        int iD = Fetch.GetID("AdminID");
        DataTable adminByAdminName = this.m_admin.GetAdminByAdminName(this.TxtUserName.Text);
        if (iD == 0)
        {
            if (adminByAdminName.Rows.Count > 0)
            {
                this.WriteErrMsg("", "此管理员已存在!", "");
            }
        }
        else
        {
            if (adminByAdminName.Rows.Count > 0)
            {
                if (iD != int.Parse(adminByAdminName.Rows[0]["Admin_ID"].ToString()))
                {
                    this.WriteErrMsg("", "此管理员已存在!", "");
                }
            }
        }
        DateTime lastLogin;
        DateTime.TryParse(this.TxtLastLogin.Text, out lastLogin);
        DateTime loginDate;
        DateTime.TryParse(this.TxtLoginDate.Text, out loginDate);
        DateTime endDate;
        DateTime.TryParse(this.TxtEndDate.Text, out endDate);
        bool disable = false;
        if (this.CheckDisable.Checked)
        {
            disable = true;
        }
        string userName = base.Server.HtmlEncode(this.TxtUserName.Text);
        string text;
        if (this.TxtPassWord.Text == "" || this.TxtPassWord.Text == null)
        {
            text = "";
        }
        else
        {
            text = base.Server.HtmlEncode(this.TxtPassWord.Text);
        }
        string text2 = "";
        string passWord = "";
        if (text != "")
        {
            text2 = this.MakePassword();
            passWord = this.m_admin.CreatePassWord(text, text2);
        }
        else
        {
            if (iD == 0)
            {
                this.WriteErrMsg("", "密码不能为空！", "");
            }
            text2 = "";
            passWord = "";
        }
        string power = this.GetPower();
        string text3 = "";
        foreach (ListItem listItem in this.ListGroup.Items)
        {
            if (listItem.Selected)
            {
                text3 = text3 + listItem.Value + ",";
            }
        }
        if (text3.Length > 1)
        {
            text3 = text3.Remove(text3.Length - 1);
        }
        int access = 0;
        if (this.CheckAccess.Checked)
        {
            access = 1;
        }
        SiteConfigBll siteConfigBll = new SiteConfigBll();
        if (siteConfigBll.GetSiteString("SiteConfigInfo/CityArea") != "0")
        {
            if (this.CheckCityArea.Checked)
            {
                text3 = "1";
            }
        }
        int loginTimes = 0;
        if (this.TxtLoginTimes.Text != "")
        {
            loginTimes = int.Parse(this.TxtLoginTimes.Text);
        }
        string provinceArea = "";
        string cityArea = "";
        if (this.CheckCityArea.Checked)
        {
            provinceArea = this.DropDownList_Province.SelectedValue;
            cityArea = this.DropDownList_City.SelectedValue;
        }
        if (e.CommandName == "add")
        {
            if (this.m_admin.AddAdmin(userName, passWord, access, this.TxtAcceptIP.Text, loginTimes, lastLogin, this.TxtLastLoginIP.Text, loginDate, this.TxtLoginIP.Text, text2, disable, endDate, this.TxtRemark.Text, text3, power, provinceArea, cityArea))
            {
                base.WriteLog("AddNew", Language.GetAdminMsg("Log/ObjectName", "0"), "Administrator");
                this.WriteSuccessMsg("", "增加管理员成功!", "Admin_Administrator.aspx");
            }
            else
            {
                this.WriteErrMsg("", "对不起，增加管理员失败，请重新操作!", "");
            }
        }
        else
        {
            if (this.m_admin.UpdateAdmin(iD, userName, passWord, this.TxtLastLoginIP.Text, text2, this.TxtAcceptIP.Text, disable, endDate, loginTimes, lastLogin, loginDate, this.TxtLoginIP.Text, access, this.TxtRemark.Text, text3, power, provinceArea, cityArea))
            {
                base.WriteLog("Modify", Language.GetAdminMsg("Log/ObjectID", iD.ToString()), "Administrator");
                if (!this.CheckCityArea.Checked)
                {
                    this.m_admin.DeleteAdminUserGroupByAdminID(iD);
                }
                this.WriteSuccessMsg("", "修改管理员成功!", "Admin_Administrator.aspx");
            }
            else
            {
                this.WriteErrMsg("", "对不起，修改管理员失败，请重新操作!", "");
            }
        }
    }
    protected string getcb(CheckBox cb)
    {
        string result;
        if (cb.Checked)
        {
            result = "1";
        }
        else
        {
            result = "0";
        }
        return result;
    }
    protected void setcb(CheckBox cb, string val)
    {
        try
        {
            if (val.Trim() == "1")
            {
                cb.Checked = true;
            }
            else
            {
                cb.Checked = false;
            }
        }
        catch
        {
        }
    }
    protected CheckBox[] GetCheckBoxes()
    {
        return new CheckBox[]
		{
			this.cb_MS,
			this.cb_Channel,
			this.cb_announce,
			this.cb_advertisement,
			this.cb_conformity,
			this.cb_CheckUser,
			this.cb_Pass,
			this.cb_Group,
			this.cb_ShortMessage,
			this.cb_Email,
			this.cb_ShopArticle,
			this.cb_Project,
			this.cb_SiteSkin,
			this.cb_Team,
			this.cb_DefindLable,
			this.cb_DeFindPage,
			this.cb_SetiSpecial,
			this.cb_ShopManage,
			this.cb_ShopMessage,
			this.cb_ShopRequest,
			this.cb_ShopProduct,
			this.cb_ShopOrder,
			this.cb_ShopSale,
			this.cb_ShopReview,
			this.cb_Pic,
			this.cb_ShopArea,
			this.cb_ShopCard,
			this.cb_ShopMoney,
			this.cb_SitePayType,
			this.cb_ShopBank,
			this.cb_ShopPayType,
			this.cb_ShopDelive,
			this.cb_ShopVipUser,
			this.cb_ShopVipLevel,
			this.cb_ShopSetup,
			this.cb_MobileMessage,
			this.cb_SubSite,
			this.cb_Witkey,
			this.cb_vipcardmanage,
			this.cb_giftcardmanage,
			this.cb_lotterymanage,
			this.cb_tryclothesmanage,
			this.cb_couponmanage,
			this.cb_financemanage,
			this.cb_useclientrelation,
			this.cb_stockmanage,
			this.cb_jxcmanage
		};
    }
    protected string GetPower()
    {
        StringBuilder stringBuilder = new StringBuilder();
        CheckBox[] checkBoxes = this.GetCheckBoxes();
        int i;
        for (i = 0; i < checkBoxes.Length - 1; i++)
        {
            stringBuilder.Append(this.getcb(checkBoxes[i]) + "|");
        }
        stringBuilder.Append(this.getcb(checkBoxes[i]));
        return stringBuilder.ToString();
    }
    protected void SetPower(string Power)
    {
        string[] array = Power.Split(new char[]
		{
			'|'
		});
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = array[i].Substring(0, 1);
        }
        try
        {
            CheckBox[] checkBoxes = this.GetCheckBoxes();
            for (int i = 0; i < checkBoxes.Length; i++)
            {
                this.setcb(checkBoxes[i], array[i]);
            }
        }
        catch
        {
        }
    }
}
